package com.ifast;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ifast.common.dao.DictDao;
import com.ifast.common.domain.DictDO;
import com.ifast.common.service.DictService;
import com.ifast.common.service.impl.DictServiceImpl;
import com.ifast.common.utils.DicSet;
import com.ifast.common.utils.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用@WebListener注解，实现ServletContextListener接口
 *
 * @author   单红宇(365384722)
 * @myblog  http://blog.csdn.net/catoop/
 * @create    2016年1月6日
 */
@WebListener
public class MyServletContextListener implements ServletContextListener {

    private static Logger log = LoggerFactory.getLogger(MyServletContextListener.class);

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("ServletContex初始化，查询加载字典值");
        log.info(sce.getServletContext().getServerInfo());
        try {
            // 字典
            log.info("________________加载字典开始_______________");
            DictDao dicMgr = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(DictDao.class);
            Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
            wrapper.orderBy(true, "sort", true);
            List<DictDO> dicList = dicMgr.selectList(wrapper);
            DicSet.getInstanse(dicList, sce.getServletContext());
            log.info("________________加载字典结束_______________");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("ServletContex销毁");
    }
 
}